<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="customerid">
      <el-input v-model="dataForm.customerid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="paytype">
      <el-input v-model="dataForm.paytype" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="salesbacktime">
      <el-input v-model="dataForm.salesbacktime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="salebackprice">
      <el-input v-model="dataForm.salebackprice" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="operateperson">
      <el-input v-model="dataForm.operateperson" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="number">
      <el-input v-model="dataForm.number" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="remark">
      <el-input v-model="dataForm.remark" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="goodsid">
      <el-input v-model="dataForm.goodsid" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          customerid: '',
          paytype: '',
          salesbacktime: '',
          salebackprice: '',
          operateperson: '',
          number: '',
          remark: '',
          goodsid: ''
        },
        dataRule: {
          customerid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          paytype: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          salesbacktime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          salebackprice: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          operateperson: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          number: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          goodsid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/warehouse/salesback/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.customerid = data.salesback.customerid
                this.dataForm.paytype = data.salesback.paytype
                this.dataForm.salesbacktime = data.salesback.salesbacktime
                this.dataForm.salebackprice = data.salesback.salebackprice
                this.dataForm.operateperson = data.salesback.operateperson
                this.dataForm.number = data.salesback.number
                this.dataForm.remark = data.salesback.remark
                this.dataForm.goodsid = data.salesback.goodsid
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/warehouse/salesback/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'customerid': this.dataForm.customerid,
                'paytype': this.dataForm.paytype,
                'salesbacktime': this.dataForm.salesbacktime,
                'salebackprice': this.dataForm.salebackprice,
                'operateperson': this.dataForm.operateperson,
                'number': this.dataForm.number,
                'remark': this.dataForm.remark,
                'goodsid': this.dataForm.goodsid
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
